<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input v-model="params.name" placeholder="请输入商品名称" style="width: 200px"></el-input>
      <el-input v-model="params.seller" placeholder="请输入卖家名字" style="width: 200px; margin-left: 5px"></el-input>
      <el-select v-model="params.typeId" placeholder="请选择" style="width: 200px;margin-left: 5px">
        <el-option v-for="item in typeObjs" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <el-button style="margin-left: 10px" type="primary" @click="findBySearch()">查询</el-button>
      <el-button style="margin-left: 10px" type="warning" @click="reset()">清空</el-button>
      <el-button v-if="user.role !== 'ROLE_STUDENT'" style="margin-left: 10px" type="success" @click="add()">新增
      </el-button>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="商品图片">
          <template v-slot="scope">
            <el-image
                :preview-src-list="['http://localhost:8081/api/files/' + scope.row.img]"
                :src="'http://localhost:8081/api/files/' + scope.row.img"
                style="width: 70px; height: 70px; border-radius: 50%">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="卖家id" prop="sellerId" v-if="user.role ==='boss'"></el-table-column>
        <el-table-column label="商品名称" prop="name"></el-table-column>
        <el-table-column label="商品卖家" prop="seller"></el-table-column>
        <el-table-column label="商品原价" prop="price"></el-table-column>
        <el-table-column label="商品现价" prop="nprice"></el-table-column>
        <el-table-column label="剩余数量" prop="num"></el-table-column>
        <el-table-column label="商品分类" prop="typeName"></el-table-column>
        <el-table-column label="商品介绍">
          <template slot-scope="scope">
            <el-button type="success" @click="viewEditor(scope.row.content)">点击查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-button v-if="user.role === 'ROLE_ADMIN'" type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-button type="primary" @click="buy(scope.row)">购买</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button v-if="user.role === 'ROLE_ADMIN'" slot="reference" style="margin-left: 5px" type="danger">
                删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px">
      <el-pagination
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <div>
      <el-dialog :visible.sync="dialogFormVisible" title="请填写信息" width="50%">
        <el-form :model="form">
          <el-form-item label="商品名称" label-width="20%">
            <el-input v-model="form.name" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="商品图片" label-width="20%">
            <el-upload :on-success="successUpload" action="http://localhost:8081/api/files/upload">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品卖家" label-width="20%">
            <el-input v-model="form.seller" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="商品原价" label-width="20%">
            <el-input v-model="form.price" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="商品现价" label-width="20%">
            <el-input v-model="form.nprice" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="剩余数量" label-width="20%">
            <el-input v-model="form.num" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" label-width="20%">
            <el-select v-model="form.typeId" placeholder="请选择" style="width: 90%">
              <el-option v-for="item in typeObjs" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品介绍" label-width="20%">
            <div id="editor" style="width: 90%"></div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-dialog :visible.sync="editorVisible" title="商品介绍" width="50%">
      <div class="w-e-text" v-html="this.viewData"></div>
    </el-dialog>
    <div>
      <el-dialog :visible.sync="dialoguserFormVisible" title="请填写信息" width="30%">
        <el-form ref="buyform" :model="buyform"  label-width="100px">
          <el-form-item label="买家名称" prop="buyer" >
            <el-input v-model="buyform.buyer" style="width: 90%" disabled></el-input>
          </el-form-item>
          <el-form-item label="送货地址" prop="name">
            <el-input v-model="buyform.area" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="name">
            <el-input v-model="buyform.telephone" style="width: 90%"></el-input>
          </el-form-item>
          <el-form-item label="具体要求" prop="description">
            <el-input v-model="buyform.description" style="width: 90%"  ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialoguserFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="reserve()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import E from "wangeditor"


let editor

function initWangEditor(content) {
  setTimeout(() => {
    if (!editor) {
      editor = new E('#editor')
      editor.config.placeholder = '请输入内容'
      editor.config.uploadFileName = 'file'
      editor.config.uploadImgServer = 'http://localhost:8081/api/files/wang/upload'
      editor.create()
    }
    editor.txt.html(content)
  }, 0)
}

export default {
  data() {
    return {
      params: {
        name: '',
        seller: '',
        typeId: '',
        pageNum: 1,
        pageSize: 5,
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      dialoguserFormVisible: false,
      editorVisible: false,
      form: {},
      buyform: {},
      viewData: '',
      typeObjs: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  // 页面加载的时候，做一些事情，在created里面
  created() {
    this.findBySearch();
    this.findTypes();
  },
  // 定义一些页面上控件出发的事件调用的方法
  methods: {
    findTypes() {
      request.get("/type").then(res => {
        if (res.code === '0') {
          this.typeObjs = res.data;
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    findBySearch() {
      request.get("/commodity/search", {
        params: this.params
      }).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    add() {
      this.form = {};
      initWangEditor("");
      this.dialogFormVisible = true;
    },
    edit(obj) {
      this.form = obj;
      initWangEditor(obj.content ? this.form.content : "");
      this.dialogFormVisible = true;
    },
    viewEditor(data) {
      this.viewData = data;
      this.editorVisible = true;
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 5,
        name: '',
        seller: '',
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    submit() {
      this.form.content = editor.txt.html();
      this.form.sellerId = this.user.id
      request.post("/commodity", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("操作成功");
          this.dialogFormVisible = false;
          this.findBySearch();
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    del(id) {
      request.delete("/commodity/" + id).then(res => {
        if (res.code === '0') {
          this.$message.success("删除成功");
          this.findBySearch();
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    successUpload(res) {
      this.form.img = res.data;
    },
    reserve() {
      let param = {
        commodityId: this.buyform.commodityId,
        userId: this.user.id,
        area: this.buyform.area,
        telephone: this.buyform.telephone,
        description: this.buyform.description,
        sellerId: this.buyform.sellerId,
      }
      request.post("/reserve", param).then(res => {
        if (res.code === '0') {
          this.$message.success("购买成功")
          this.dialoguserFormVisible = false;
          this.findBySearch()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    buy(row) {
      this.buyform = {commodityId:row.id,sellerId:row.sellerId};
      this.buyform.buyer = this.user.name;
      this.dialoguserFormVisible = true;
    }

  }
}
</script>
